//
//  YeeUserInfoData.m
//  SmallLook
//
//  Created by CoderYee on 2017/2/21.
//  Copyright © 2017年 余伟. All rights reserved.
//

#import "YeeUserInfoManger.h"
#ifndef APP_EXT
#import "UIImageView+WebCache.h"
#endif
#define APP_GROUP        @"group.com.tencent.fx.rtmpdemo"
#define kUserInfoKey     @"kUserInfoKey"
static YeeUserInfoManger *_shareInstance = nil;
@interface YeeUserInfoManger ()
{
    NSString        *_identifier;
    YeeUserProfile  *_userInfo;
}
@end
@implementation YeeUserInfoManger
- (instancetype)init
{
    self = [super init];
    if (self)
    {
        _userInfo  = [[YeeUserProfile alloc] init];
    }
    return self;
}
- (void)dealloc
{
    
}
+ (instancetype)sharedInstance
{
    static dispatch_once_t predicate;
    
    dispatch_once(&predicate, ^{
        _shareInstance = [[YeeUserInfoManger alloc] init];
    });
    return _shareInstance;
}
/**
 *  保存用户ID信息,并且注册回调通知,当收到登陆成功通知后拉取用户信息
 *  数据拉取成功后存入 userInfo  结构当中
 */
- (void)setIdentifier:(NSString *)identifier
{
    _identifier = identifier;
#ifndef APP_EXT
    [self fetchUserInfo];
#endif
}

#pragma mark 从服务器上拉取信息
#pragma mark 通过id信息从服务器上拉取用户信息
-(void)fetchUserInfo
{
    //[self setUserProfile:friends[0]];
}
// 从内存中读取
- (YeeUserProfile*)getUserProfile
{
    return _userInfo;
}
// 从文件中读取
- (YeeUserProfile*)loadUserProfile
{
    
    YeeUserProfile *info = [[YeeUserProfile alloc] init];
    
    // 从文件中读取
    NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:APP_GROUP];
    if (defaults == nil) {
        defaults = [NSUserDefaults standardUserDefaults];
    }
    NSString *useridKey = [NSString stringWithFormat:@"%@_UserInfo", _identifier];
    if (useridKey) {
        NSString *strUserInfo = [defaults objectForKey:useridKey];
        NSData *data = [strUserInfo dataUsingEncoding:NSUTF8StringEncoding];
        NSError *err = nil;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&err];
        if (dic) {
            info.identifier = [_identifier copy];
            info.nickname = [dic objectForKey:@"nickName"];
            info.faceURL = [dic objectForKey:@"faceURL"];
        }
    }
    return info;
}

//这里保存数据
- (void)setUserProfile:(YeeUserProfile *)profile
{
    
    if ( 0 == profile.nickname.length)
    {
        _userInfo.nickname   = profile.identifier;
    }
    else
    {
        _userInfo.nickname   = profile.nickname;
    }
    
    _userInfo.identifier = profile.identifier;
    _userInfo.faceURL    = profile.faceURL;
    [self saveToLocal];
    
}

- (void)saveToLocal {
    // 保存昵称，头像，封页, 性别 到本地，方便其他进程读取
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    [dic setObject:_userInfo.nickname forKey:@"nickName"];
    [dic setObject:_userInfo.faceURL forKey:@"faceURL"];

    NSError *error = nil;
    NSData *data = [NSJSONSerialization dataWithJSONObject:dic options:0 error:&error];
    NSString *strUserInfo = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSString *useridKey   = [NSString stringWithFormat:@"%@_UserInfo", _identifier];
    NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:APP_GROUP];
    if (defaults == nil)
    {
        defaults = [NSUserDefaults standardUserDefaults];
    }
    [defaults setObject:useridKey forKey:kUserInfoKey];
    [defaults setObject:strUserInfo forKey:useridKey];
    [defaults synchronize];
}

@end
